#!/bin/bash
# some default values
N=10
level=3
type=STD
name=""
Parallel=0

# parse args
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -h|--help)
	echo "Usage MakeBook.sh [options]"
	echo "Options:"
	echo "    -h|--help:         Print this help"
	echo "    -l|--level:        Set the sudoku level"
	echo "    -t|--type:         Set the sudoku type. Valid types are STD, NRC, NRCX, CUBE 3DCUBE"
	echo "    -o|--out:          Base name of the book"
	echo "    -n|--number:       Number of sudokus in the book"
	echo "    -p|--parallel:     Generate sudokus in parallel"
	shift
	;;	
    -p|--parallel)	
	Parallel=1;
	shift
	;;
    -l|--level)
    level="$2"
    shift # past argument
    shift # past value
    ;;
    -t|--type)
    type="$2"
    shift # past argument
    shift # past value
    ;;
    -o|--out)
    name=$2
    shift # past argument
    shift # past value
    ;;
    -n|--number)
    N="$2"
    shift # past argument
    shift # past value
    ;;
    *)    # unknown option
    echo "Unknown option \"$1\""
    exit
    ;;
esac
done
# set remaining arguments
case $type in 
    STD)
    template=STD.gss
    title=Standard
    if [ -z "$name" ]
    then
		name=stdbook
	fi
    ;;
    NRCX)
    template=NRCX.gss
    title=NRC-X
    if [ -z "$name" ]
    then
		name=nrcxbook
	fi
    ;;
    NRC)
    title=NRC
    template=NRC.gss
    if [ -z "$name" ]
    then
		name=nrcbook
	fi
    ;;
    CUBE)
    template=bigcube.gss
    title="Big Cube"
    if [ -z "$name" ]
    then
		name=cubebook
	fi
    ;;
    JIGSAW)
    title="Jigsaw"
    if [ -z "$name" ]
    then
		name=jigbook
	fi
    ;;
    JIGSAW-XL)
    title="Jigsaw-XL"
    if [ -z "$name" ]
    then
		name=jigbook-XL
	fi
    ;;
    JIGCUBE)
    title="JigCube"
    if [ -z "$name" ]
    then
		name=jigcube
	fi
    ;;
    3DCUBE)
    title="3D cube"
    template=3DCube.gss
    if [ -z "$name" ]
    then
		name=3dcubebook
	fi
    ;;
    PARQ)
    title="Parquet"
    template=parquet.gss
    if [ -z "$name" ]
    then
		name=parquet
	fi
    ;;
    LINK)
    title="Hyper Linked"
    template=Linked.gss
    if [ -z "$name" ]
    then
		name=linked
	fi
    ;;
    *)    
    echo "Error: unknown sudoku type $type"
    exit
    ;;
esac

echo "---------------------------------------------------------------------------"
echo "Creating a book called $name with $N sudokus of type $type and level $level"
echo "---------------------------------------------------------------------------"
# generate puzzles and solutions
if [ "$type" == "JIGSAW" ] || [ "$type" == "JIGSAW-XL" ] || [ "$type" == "JIGCUBE" ] 
then
	if [ "$Parallel" -eq 0 ]
	then		
		case $type in 
			JIGSAW)
			for i in $(seq -w 1 $N)
			do
				./genjigsaw.sh -o jig.gss -s 9
				gss -t jig.gss -g $level -s solution$i.asy -p puzzle$i.asy -F
			done
			;;
			JIGSAW-XL)
			for i in $(seq -w 1 $N)
			do
				./genjigsaw.sh -o jig.gss -s 16
				gss -t jig.gss -g $level -s solution$i.asy -p puzzle$i.asy -F
			done
			;;
			JIGCUBE)
			for i in $(seq -w 1 $N)
			do
				./genbigjigcube.sh -o jig.gss
				gss -t jig.gss -g $level -s solution$i.asy -p puzzle$i.asy -F
			done
			;;
		esac
	else		
		case $type in 
			JIGSAW)
			seq -w 1 $N | parallel ./genjigsaw.sh -o jig{}.gss -s 9
			;;
			JIGSAW-XL)
			seq -w 1 $N | parallel ./genjigsaw.sh -o jig{}.gss -s 16
			;;
			JIGCUBE)
			seq -w 1 $N | parallel ./genbigjigcube.sh -o jig{}.gss 
			;;
		esac
		seq -w 1 $N | parallel gss -t jig{}.gss -g $level -s solution{}.asy -p puzzle{}.asy -F
		rm jig*.gss
	fi
else 
	if [ "$Parallel" -eq 0 ]
	then
		for i in $(seq -w 1 $N)
		do
			gss -t $template -g $level -s solution$i.asy -p puzzle$i.asy
		done
	else
		seq -w 1 $N | parallel gss -t $template -g $level -s solution{}.asy -p puzzle{}.asy
	fi
fi
# make the latex input file
echo "\\documentclass[12pt]{report}">"$name".tex
echo "\\usepackage[inline]{asymptote}">>"$name".tex
echo "\\usepackage{hyperref}">>"$name".tex
echo "\\newcounter{sudokucount}">>"$name".tex
echo "\\begin{document}">>"$name".tex
echo "\\begin{titlepage}">>"$name".tex
echo "\\begin{center}">>"$name".tex
echo "\\vspace*{5cm} ">>"$name".tex
echo "{\\Huge\\textbf{$title Sudoku Book}}\\\\">>"$name".tex
echo "\\vspace{1cm}">>"$name".tex
echo "\\textbf{Generated by gss}\\\\">>"$name".tex
echo "\\vspace{3cm}">>"$name".tex
echo "$N $title sudokus of level $level ">>"$name".tex
echo "\\end{center}">>"$name".tex
echo "\\end{titlepage}">>"$name".tex
for i in $(seq -w 1 $N)
do
	echo p $i
	echo "\\begin{minipage}[t]{\textwidth}">>"$name".tex
	echo "\\begin{center}">>"$name".tex
	echo "\\begin{asy}">>"$name".tex
	cat puzzle$i.asy>>"$name".tex
	echo "\\end{asy}">>"$name".tex
	echo "">>"$name".tex
	echo "">>"$name".tex
	echo "{\\bf  \refstepcounter{sudokucount} \label{puzzle$i} Sudoku $i}\\">>"$name".tex
	echo "solution on page \pageref{solution$i}\\">>"$name".tex
	echo "\\end{center}">>"$name".tex
	echo "\\end{minipage}">>"$name".tex
	echo "">>"$name".tex
done
 
for i in $(seq -w 1 $N)
do
	echo s $i
	echo "\\begin{minipage}[t]{\textwidth}">>"$name".tex
	echo "\\begin{center}">>"$name".tex
	echo "\\begin{asy}">>"$name".tex
	cat solution$i.asy>>"$name".tex
	echo "\\end{asy}">>"$name".tex
	echo "">>"$name".tex
	echo "">>"$name".tex
	echo "{\\bf \refstepcounter{sudokucount} \label{solution$i} Solution $i}\\">>"$name".tex
	echo "puzzle on page \pageref{puzzle$i}\\">>"$name".tex
	echo "\\end{center}">>"$name".tex
	echo "\\end{minipage}">>"$name".tex
	echo "">>"$name".tex
done
echo "\\end{document}">>"$name".tex
# make the figures and the book
pdflatex "$name".tex
if [ "$Parallel" -eq 0 ]
then
	asy "$name"-*.asy
else
	parallel asy ::: "$name"-*.asy
fi
pdflatex "$name".tex
pdflatex "$name".tex
rm "$name"*.pre
rm "$name"-*
rm "$name".aux
rm "$name".log
rm puzzle*.asy
rm solution*.asy
